package com.dd.ms.provider.web.service.internal;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.ms.provider.pojo.DataObjectBO;
import com.dd.ms.provider.pojo.req.DataObjectPageQueryReq;
import com.dd.ms.provider.web.converter.DataObjectConverter;
import com.dd.ms.provider.web.converter.DataObjectQryBuilder;
import com.dd.ms.provider.web.mapper.DataObjectMapper;
import com.dd.ms.provider.web.pojo.DO.DataObjectDO;
import com.dd.ms.provider.web.respository.DataObjectRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/3/11 6:38
 * @Description DataObjectServiceImpl
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DataObjectServiceImpl extends ServiceImpl<DataObjectMapper, DataObjectDO> {

    private final DataObjectRepository dataObjectRepository;

    public DataObjectBO getOneById(Long id) {
        return DataObjectConverter.convert2BO(dataObjectRepository.getOne(id));
    }

    public Page<DataObjectBO> pageFeign(DataObjectPageQueryReq req) {
        Page<DataObjectDO> page = new Page<>(req.getPageParams().getPageNum(), req.getPageParams().getPageSize());
        Page<DataObjectDO> dataObjectDOPage = this.page(page, DataObjectQryBuilder.buildLQW(req.getQueryParams()));
        List<DataObjectBO> dataObjectBOList = dataObjectDOPage.getRecords().stream()
                .map(DataObjectConverter::convert2BO).collect(Collectors.toList());

        Page<DataObjectBO> dataObjectBOPage = new Page<>();
        BeanUtils.copyProperties(dataObjectDOPage, dataObjectBOPage);
        return dataObjectBOPage.setRecords(dataObjectBOList);
    }
}
